package org.danger.t7.common.ws;

import java.util.Map;

import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

import org.danger.t7.common.spring.SpringContext;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * WebSocket配置类，这里怎么支持@AutoWired ？
 */
public class FkWebSocketConfigurator extends ServerEndpointConfig.Configurator {

    /**
     * 功能描述:modifyHandshake
     *
     * @param sec      .
     * @param request  .
     * @param response .
     */
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
//        String uid = request.getParameterMap().get("uid").get(0);
        WsEventHandler handler = SpringContext.getBean(WsEventHandler.class);
        String uid = handler.handshake(request, response);
        if (uid != null && !uid.equals("")) {
            Map<String, Object> userProperties = sec.getUserProperties();
            userProperties.put("uid", uid);
        }

        //从httpSession中取出userId放入websocket的session中
//        HttpSession session = (HttpSession) request.getHttpSession();
//        if (session != null) {
//            Enumeration<String> names = session.getAttributeNames();
//            while (names.hasMoreElements()) {
//                String name = names.nextElement();
//                userProperties.put(name, session.getAttribute(name));
//            }
//        }
    }
}
